﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Dynamics.BusinessConnectorNet;

namespace PJ.Data.DynamicsAX
{
    internal sealed class DynamicsAXBroker : IDisposable
    {
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        public static AxaptaContainer CreateContainer()
        {
            return Instance.Axapta.CreateAxaptaContainer();
        }

        public static object StaticMethod(string className, string method)
        {
            return Instance.CallStaticMethod(className, method);
        }
        public static object StaticMethod(string className, string method, object argument)
        {
            return Instance.CallStaticMethod(className, method, argument);
        }
        public static object StaticMethod(string className, string method, object argument1, object argument2)
        {
            return Instance.CallStaticMethod(className, method, argument1, argument2);
        }
        public static object StaticMethod(string className, string method, object argument1, object argument2, object argument3)
        {
            return Instance.CallStaticMethod(className, method, argument1, argument2, argument3);
        }
        public static object StaticMethod(string className, string method, object[] args)
        {
            return Instance.CallStaticMethod(className, method, args);
        }

        private object CallStaticMethod(string className, string method)
        {
            return Instance.Axapta.CallStaticClassMethod(className, method);
        }
        private object CallStaticMethod(string className, string method, object argument)
        {
            return Instance.Axapta.CallStaticClassMethod(className, method, argument);
        }
        private object CallStaticMethod(string className, string method, object argument1, object argument2)
        {
            return Instance.Axapta.CallStaticClassMethod(className, method, argument1, argument2);
        }
        private object CallStaticMethod(string className, string method, object argument1, object argument2, object argument3)
        {
            return Instance.Axapta.CallStaticClassMethod(className, method, argument1, argument2, argument3);
        }
        private object CallStaticMethod(string className, string method, object[] args)
        {
            return Instance.Axapta.CallStaticClassMethod(className, method, args);
        }

        private static DynamicsAXBroker Instance
        {
            get
            {
                return m_Instance;
            }
        }
        private Axapta Axapta
        {
            get
            {
                if (m_Axapta == null)
                {
                    m_Axapta = new Axapta();
                    Open();
                }
                return m_Axapta;
            }
        }
        private void Open()
        {
            Axapta.Logon("", "", "", "");
        }
        private void Close()
        {
            if (m_Axapta != null)
            {
                m_Axapta.Logoff();
            }
        }

        private bool Opened
        {
            get
            {
                if (m_Axapta == null)
                    return false;
                return m_Axapta.Session() != 0;
            }
            set
            {
                if (value && !Opened)
                    Open();
                if (!value && Opened)
                    Close();
            }
        }


        private void Dispose(bool disposable)
        {
            if (m_Axapta != null && disposable)
            {
                m_Axapta.Dispose();
                m_Axapta = null;
            }
        }

        private Axapta m_Axapta;
        private static readonly DynamicsAXBroker m_Instance = new DynamicsAXBroker();


    }
}
